<template>
    <div class="app-container">
        <!-- 搜索 -->
        <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch">
            <el-form-item label="批次号" prop="batchNumber">
                <el-input v-model="queryParams.batchNumber" placeholder="请输入产品名称" clearable
                    @keyup.enter.native="handleQuery" />
            </el-form-item>
            <el-form-item label="产品名称" prop="productName">
                <el-input v-model="queryParams.productName" placeholder="请输入产品名称" clearable
                    @keyup.enter.native="handleQuery" />
            </el-form-item>


            <el-form-item label="叶片类型" prop="bladeType">
                <el-select v-model="queryParams.bladeType" placeholder="请选择叶片类型">
                    <el-option v-for="dict in dict.type.sye_product_blade" :key="dict.label" :label="dict.label"
                        :value="dict.value"></el-option>
                </el-select>
            </el-form-item>
            <el-form-item label="驱动方式" prop="driveMode">
                <el-select v-model="queryParams.driveMode" placeholder="请选择驱动方式">
                    <el-option v-for="dict in dict.type.sys_product_drive" :key="dict.label" :label="dict.label"
                        :value="dict.value"></el-option>
                </el-select>
            </el-form-item>
            <el-form-item label="生产小组" prop="groupId">
                <el-select v-model="queryParams.groupId" placeholder="请选择生产小组">
                    <el-option :label="item.groupName" :value="item.id" v-for="(item, index) in groupList"
                        :key="index"></el-option>
                </el-select>
            </el-form-item>
            <el-form-item label="是否加急">
                <el-select v-model="queryParams.priority" placeholder="请选择">
                    <el-option v-for="item in dict.type.sys_isno" :key="item.value" :label="item.label"
                        :value="item.value">
                    </el-option>
                </el-select>
            </el-form-item>
            <el-form-item label="批次状态">
				<el-select v-model="queryParams.status" placeholder="请选择">
					<el-option v-for="item in dict.type.self_batch_status" :key="item.value" :label="item.label"
						:value="item.value">
					</el-option>
				</el-select>
			</el-form-item>
            <el-form-item label="创建时间">
                <el-date-picker v-model="dateRange" style="width: 240px" value-format="yyyy-MM-dd" type="daterange"
                    range-separator="-" start-placeholder="开始日期" end-placeholder="结束日期"></el-date-picker>
            </el-form-item>
            <input hidden />
            <el-form-item>
                <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
                <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
            </el-form-item>
        </el-form>
        <!-- 工具栏 -->
        <el-row :gutter="10" class="mb8">
            <el-col :span="1.5">
                <el-button  :disabled="single" type="warning" plain icon="el-icon-download" size="mini" @click="handleExports(1)"
                    v-hasPermi="['batch:export:BaiYe']">导出百叶标签</el-button>
                <el-button :disabled="single" type="warning" plain icon="el-icon-download" size="mini" @click="handleExports(2)"
                    v-hasPermi="['batch:export:BoLi']">导出玻璃订单</el-button>
            </el-col>
            <right-toolbar :showSearch.sync="showSearch" @queryTable="getDataList"></right-toolbar>
        </el-row>
        <!-- 表格 -->
        <el-table v-loading="loading" :data="dataList" @selection-change="handleSelectionChange">
            <el-table-column type="selection" width="55" align="center" />
            <el-table-column label="ID" prop="id" width="60" />
            <el-table-column label="批次号" prop="batchNumber" width="140" />
            <el-table-column label="产品名称" prop="productName" width="140" />
            <el-table-column label="叶片类型" prop="bladeType" width="100" />
            <el-table-column label="驱动方式" prop="driveMode" width="100" />
            <el-table-column label="合计件数" prop="productCountSum" width="100" />
            <el-table-column label="批次状态" prop="status" width="100">
                <template slot-scope="scope">
					<dict-tag :options="dict.type.self_batch_status" :value="scope.row.status" />
				</template>
            </el-table-column>

            <el-table-column label="是否加急" prop="priority" width="100">
                <template slot-scope="scope">
                    <el-tag v-if="scope.row.priority == '1'" size="medium">是</el-tag>
                    <el-tag v-else size="medium" type="info">否</el-tag>
                </template>
            </el-table-column>
            <el-table-column label="合计数量" prop="productCount" width="100" />
            <el-table-column label="磁铁使用数量" prop="countCt" width="100" />
            <el-table-column label="电机使用数量" prop="countDj" width="100" />
            <el-table-column label="型材使用数量" prop="countXc" width="100" />

            <el-table-column label="百叶标签是否打印" prop="printTag" width="140">
                <template slot-scope="scope">
                    是否打印
                    <el-switch v-model="scope.row.printTag" active-value="1" inactive-value="0"
                        @change="handleStatusChangeby(scope.row)"></el-switch>
                </template>
            </el-table-column>
            <el-table-column label="玻璃订单是否下单" prop="glassOrder" width="140">
                <template slot-scope="scope">
                    是否下单
                    <el-switch v-model="scope.row.glassOrder" active-value="1" inactive-value="0"
                        @change="handleStatusChange(scope.row)"></el-switch>
                </template>
            </el-table-column>
            <el-table-column label="生产小组" prop="groupName" width="100" />
            <el-table-column label="创建时间" prop="createTime" width="180" />
            <el-table-column label="分配时间" prop="groupTime" width="180" />
            <el-table-column label="完成时间" prop="statusTime" width="180">
                <template slot-scope="scope">
                    <div>{{ scope.row.status == '40' ? scope.row.statusTime : '' }}</div>
                </template>

            </el-table-column>
            <el-table-column label="操作" align="center" width="220" class-name="small-padding fixed-width" fixed="right">
                <template slot-scope="scope">
                    <el-button size="mini" type="text" icon="el-icon-view"
                        @click="handleDetail(scope.row)" v-hasPermi="['batch:info']">详情</el-button>
                    <el-button size="mini" type="text" icon="el-icon-edit" @click="handleUpdate(scope.row)"
                        v-if="scope.row.groupName == null" v-hasPermi="['batch:edit']">修改</el-button>
                    <el-button size="mini" type="text" @click="handleFp(scope.row)"
                        v-if="scope.row.groupName == null" v-hasPermi="['batch:set:group']">分配小组</el-button>
                    <el-button size="mini" type="text" icon="el-icon-download"
                        @click="handleExport(scope.row)" :style="{color:scope.row.downloadCountBatchOrder>0?'#771CAA':''}" v-hasPermi="['batch:print:batch']">下载生产批次单</el-button>

                        
                </template>
            </el-table-column>
        </el-table>
        <!-- 分页 -->
        <pagination v-show="total > 0" :total="total" :page.sync="queryParams.pageNum"
            :limit.sync="queryParams.pageSize" @pagination="getDataList" />
        <!-- 排产 -->
        <el-dialog title="分配生产:注意（分配小组之后任何信息将不能被修改）" :visible.sync="planShow" width="500px" append-to-body>
            <!-- <div style="font-size: 20px;font-weight: bold;color: red;margin-bottom: 20px;">分配小组之后信息不能修改</div> -->
            <el-form ref="formref" :model="form" :rules="rules" size="medium" label-width="100px">
                <el-form-item label="生产小组" prop="groupId">
                    <el-select v-model="form.groupId" placeholder="请选择生产小组">
                        <el-option :label="item.groupName" :value="item.id" v-for="(item, index) in groupList"
                            :key="index"></el-option>
                    </el-select>
                </el-form-item>
            </el-form>
            <div slot="footer">
                <el-button @click="planCancel">取消</el-button>
                <el-button type="primary" @click="planConfirm">确定</el-button>
            </div>
        </el-dialog>
    </div>
</template>

<script>
import { getList, getInfo, editList, setGroup, setBaiYe, setBoLi } from '@/api/batch'
import {
    postUserGroupList
} from '@/api/group'
export default {
    dicts: ['self_batch_status', 'sye_product_blade', 'sys_product_drive', 'sys_isno'],
    data() {
        return {
            // 遮罩层
            loading: true,
            // 选中数组
            ids: [],
            // 非单个禁用
            single: true,
            // 非多个禁用
            multiple: true,
            // 显示搜索条件
            showSearch: true,
            // 总条数
            total: 0,
            // 查询参数
            queryParams: {
                pageNum: 1,
                pageSize: 10,
                batchNumber: undefined, // 平台名字
                productName: undefined, // 平台编号
                bladeType: undefined, // 平台编号
                driveMode: undefined,
                groupId: undefined, // 平台编号
                priority: undefined,
                status: undefined,
                createTime: undefined,
            },
            // 数据列表
            dataList: [],
            // 生产角色下所有用户列表
            scUserList: [],
            // 添加弹框标题
            addDialogTitle: '',
            // 添加弹框是否显示
            addDialogVisible: false,
            setparam: false,
            // 添加弹框表单参数
            form: {
                groupId: ''
            },
            // 添加弹框表单校验
            rules: {
                groupId: [{ required: true, message: '请选择生产小组', trigger: 'blur' }],
            },
            planShow: false,
            groupList: [],
            dateRange: [],

        }
    },
    created() {
        this.getDataList()
        this.getprolist()
    },
    methods: {
        // 获取分组
        getprolist() {
            postUserGroupList({ pageNum: 1, pageSize: 10000 }).then(res => {
                this.groupList = res.rows
            })
        },
        /** 搜索按钮操作 */
        handleQuery() {
            this.queryParams.pageNum = 1
            this.getDataList()
        },
        /** 重置按钮操作 */
        resetQuery() {
            this.queryParams = {
                pageNum: 1,
                pageSize: 10,
                batchNumber: undefined, // 平台名字
                productName: undefined, // 平台编号
                bladeType: undefined, // 平台编号
                driveMode: undefined,
                groupId: undefined, // 平台编号
                priority: undefined,
                createTime: undefined,
            }
            this.dateRange = [];

            this.resetForm('queryForm')
            this.handleQuery()
        },
        /** 多选框选中数据 */
        handleSelectionChange(selection) {
            this.ids = selection.map((item) => item.id)
            this.single = !selection.length
            this.multiple = !selection.length
        },
        /** 查询列表 */
        getDataList() {
            this.queryParams.beginTime = this.dateRange[0];
            this.queryParams.endTime = this.dateRange[1];
            this.loading = true
            getList(this.queryParams).then((response) => {
                this.dataList = response.rows
                this.total = response.total
                this.loading = false
            })
        },
        handleUpdate(row) {
            this.$router.push('/batch/edit/' + row.id)
        },
        handleDetail(row) {
            this.$router.push('/batch/detail/' + row.id)
        },
        handleFp(row) {
            this.form.id = row.id
            this.planShow = true
        },
        planConfirm: function () {
            this.$refs.formref.validate((valid) => {
                if (valid) {
                    setGroup(this.form).then(res => {
                        if (res.code == 200) {
                            this.$modal.msgSuccess("分配成功");
                            this.planShow = false
                            this.getDataList()
                        }
                    })
                }
            });
        },
        planCancel() {
            this.form = {
                groupId: ''
            }
            this.planShow = false
        },
        handleExports(i) {
            if (i == 1) {
                this.download(
                    "batch/export/BaiYe",
                    {ids:this.ids },
                    `百叶标签信息.xlsx`
                );
            }else{
                this.download(
                    "batch/export/BoLi",
                    {ids:this.ids },
                    `玻璃订单信息.xlsx`
                );
            }
    },
    handleExport(row) {
        this.download(
            "batch/print/batch",
            { id: row.id },
            `生产批次单.xlsx`
        );
    },
    handleStatusChange(e) {
        let data = { id: e.id, glassOrder: e.glassOrder }
        setBoLi(data).then(res => {
            if (res.code == 200) {
                this.$modal.msgSuccess("已修改");
                this.getDataList()
            }
        })
    },
    handleStatusChangeby(e) {
        let data = { id: e.id, printTag: e.printTag }
        setBaiYe(data).then(res => {
            if (res.code == 200) {
                this.$modal.msgSuccess("已修改");
                this.getDataList()
            }
        })
    }
}
}
</script>

<style></style>